package org.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.zhxy.model.entity.Admin;
import org.zhxy.model.result.Result;
import org.zhxy.service.AdminService;

import java.util.List;

@RestController
@RequestMapping("/sms/adminController")
@RequiredArgsConstructor
@Slf4j
public class AdminController {
    private final AdminService adminService;

    @Operation(summary = "分页条件查询管理员信息")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(@PathVariable Integer pageNo, @PathVariable Integer pageSize, String adminName) {
        log.info("分页条件查询管理员信息，请求参数：{},{},{}", pageNo, pageSize, adminName);

        IPage<Admin> page = new Page<>(pageNo, pageSize);
        adminService.page(page, adminName);
        log.info("分页条件查询管理员信息，响应数据：{}", page);

        return Result.ok(page);
    }

    @Operation(summary = "保存或更新管理员信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@RequestBody Admin admin) {
        log.info("保存或更新管理员信息，请求参数：{}", admin);

        if (!Strings.isEmpty(admin.getPassword())) {
            String passwd = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
            admin.setPassword(passwd);
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

    @Operation(summary = "批量删除管理员信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody List<Integer> ids) {
        log.info("批量删除管理员信息，请求参数：{}", ids);

        adminService.removeByIds(ids);
        return Result.ok();
    }

}
